package com.hamizhong.iflowers.web.action;

import java.util.Date;
import java.util.List;

import com.hamizhong.iflowers.service.IUserService;
import com.hamizhong.iflowers.utils.MD5Utils;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.UserVO;

public class DoRegistAction extends MyBaseAction {

	private IUserService iUserService;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Override
	protected String doRequest() {
		logger.info("----------------- Hello DoRegistAction -----------------");
		String registEmail = getRQ().getParameter("registEmail");
		String registPhone = getRQ().getParameter("registPhone");
		String registPassword = getRQ().getParameter("registPassword");
		String registRePassword = getRQ().getParameter("registRePassword");
		logger.info("registEmail:"+registEmail);
		logger.info("registPhone:"+registPhone);
		logger.info("registPassword:"+registPassword);
		logger.info("registRePassword:"+registRePassword);
		//验证密码
		if(null != registPassword && null != registRePassword) {
			if(!registPassword.equals(registRePassword)) {
				getRQ().setAttribute("errorMsg", "对不起，两次输入的密码不一致！");
				return INPUT;
			}
			String validPassword = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
			if(registPassword.length() >= 6 && registPassword.length() <= 10) {
				for(int i=0;i<registPassword.length();i++) {
					if(validPassword.indexOf(registPassword.charAt(i)) < 0) {
						getRQ().setAttribute("errorMsg", "对不起，请输入有效密码！");
						return INPUT;
					}
				}
			}
		}else {
			getRQ().setAttribute("errorMsg", "对不起，请输入有效密码！");
			return INPUT;
		}
		//验证电子邮箱
		IOPResultDto checkDto = iUserService.findByEmail(registEmail);
		if(checkDto.isSuccess()) {
			List<IObjectVO> iObjectVOList = checkDto.getResultList();
			if(iObjectVOList.get(0).getUserVOList().size() > 0) {
				logger.info("userVOList.size:"+iObjectVOList.get(0).getUserVOList().size());
				getRQ().setAttribute("errorMsg", "对不起，电子邮箱["+registEmail+"]已被注册！");
				return INPUT;
			}
		}
		//验证手机号
		IOPResultDto checkDto2 = iUserService.findByPhone(registPhone);
		if(checkDto2.isSuccess()) {
			List<IObjectVO> iObjectVOList = checkDto2.getResultList();
			if(iObjectVOList.get(0).getUserVOList().size() > 0) {
				getRQ().setAttribute("errorMsg", "对不起，手机号["+registPhone+"]已被注册！");
				return INPUT;
			}
		}
		UserVO userVO = new UserVO();
		Date date = new Date();
		userVO.setUserId(0);
		userVO.setUserName(registEmail);
		userVO.setUserEmail(registEmail);
		userVO.setUserPhone(registPhone);
		userVO.setUserPassword(registPassword);
		userVO.setCreateDate(date);
		userVO.setUpdateDate(date);
		IOPResultDto dto = iUserService.doRegistUser(userVO);
		if(dto.isSuccess()) {
			return SUCCESS;
		}else {
			return INPUT;
		}
	}

}
